local zhongxi = fk.CreateSkill{
  name = "xiaobai__zhongxi",
}

Fk:loadTranslationTable{
  ["xiaobai__zhongxi"] = "忠惜",  
  [":xiaobai__zhongxi"] = "你/上次“忧谏”交换牌的目标的弃牌阶段开始时，其/你可以弃置至多X张手牌令你/其手牌上限增加等量，"..
    "然后你可以令因此弃置了X张或全部手牌的角色回复1点体力或摸两张牌(X为本回合角色需弃置的牌数)。",

  ["#TargetHand"] = "对方手牌",
  ["#SelfHand"] = "你的手牌",
  ["#zhongxi-award"] = "忠惜：你可以令 %dest 回复体力或摸两张牌",
  ["#zhongxi-recoverOrNot"] = "忠惜：你可以令 %dest 回复1点体力，或点击取消令其摸两张牌",
  ["#zhongxi-use"] = "忠惜：你可以弃置至多 %arg 令 %dest 本回合手牌上限增加等量"
}

zhongxi:addEffect(fk.EventPhaseStart, {
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(self) and target.phase == Player.Discard
  end,
  on_cost = function (self, event, target, player, data)
    local to = nil
    for _, value in ipairs(player.room:getAlivePlayers()) do
      if value:getMark("youjian") > 0 then
        to = value
        break
      end
    end
    if to == nil then
      return false
    end
    local cost_data = {}
    if player.room.current == player then
      cost_data.from = player
      cost_data.to = to
    elseif player.room.current == to then
      cost_data.from = to
      cost_data.to = player
    else
      return false
    end
    local num = cost_data.from:getHandcardNum() - cost_data.from:getMaxCards()
    if num>0 then
      local cards = player.room:askToDiscard(cost_data.to, {
        min_num = 1,
        max_num = num,
        include_equip = false, 
        skill_name = zhongxi.name,
        prompt = "#zhongxi-use::"..cost_data.from.id..":"..num,
        skip = true})
      if cards and #cards>0 then
        cost_data.cards = cards
        event:setCostData(self, cost_data)
        return true
      end
    end
    return false
  end,
  on_use = function (self, event, target, player, data)
    local cost_data = event:getCostData(self)
    local to = cost_data.to
    local from = cost_data.from
    local room = player.room
    local cards = cost_data.cards
    local kongcheng = false
    if to:getHandcardNum() == #cards or #cards == cost_data.from:getHandcardNum() - cost_data.from:getMaxCards() then
      kongcheng = true
    end
    room:throwCard(cards, zhongxi.name, to, to)
    if to:isAlive() then
      room:addPlayerMark(from, MarkEnum.AddMaxCards.."-turn", #cards)
      if kongcheng then
        if room:askToSkillInvoke(player, {skill_name = zhongxi.name, prompt = "#zhongxi-award::"..to.id}) then
          if room:askToSkillInvoke(player, {skill_name = zhongxi.name, prompt = "#zhongxi-recoverOrNot::"..to.id}) then
            if to:isWounded() then
              room:recover({
                who = to,
                num = 1,
                recoverBy = player,
                skillName = zhongxi.name
              })
            end
          else
            to:drawCards(2, zhongxi.name)
          end
        end
      end
    end
  end
})

return zhongxi